brute force constructive algorithms games graphs greedy implementation sortings *2100

Please click on ads to support us..

Python Code:

def main():
    
    n, k = readIntArr()
    a = list(input())
    
    batch = []
    for i in range(n - 1):
        if a[i] == 'R' and a[i + 1] == 'L':
            batch.append(i)
    allmoves = []
    while len(batch) > 0:
        allmoves.append(batch)
        for i in batch:
            a[i], a[i + 1] = a[i + 1], a[i]
        
        batch2 = set()
        for i in batch:
            i -= 1
            if i >= 0 and a[i] == 'R' and a[i + 1] == 'L':
                batch2.add(i)
            i += 2
            if i + 1 < n and a[i] == 'R' and a[i + 1] == 'L':
                batch2.add(i)
        batch = list(batch2)
    min_moves = len(allmoves)
    max_moves = sum([len(moves) for moves in allmoves])
    if not min_moves <= k <= max_moves:
        print(-1)
        return
    
        allans = []
    before = 0
    after = len(allmoves)
    for moves in allmoves:
        after -= 1
        l = len(moves)
        nmoves = min(l, k - before - after)
        for i in range(nmoves - 1):
            allans.append((1, moves[i] + 1))
            before += 1
        arr = [len(moves) - (nmoves - 1)]
        for i in range(nmoves - 1, len(moves)):
            arr.append(moves[i] + 1)
        before += 1
        allans.append(tuple(arr))
        if len(allans) > 1000000:
            multiLineArrayOfArraysPrint(allans)
            allans = []
    if allans:
        multiLineArrayOfArraysPrint(allans)
    
    return

import sys
input=lambda: sys.stdin.readline().rstrip("\r\n")  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b):
    print('? {} {}'.format(a, b))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(ans):
    print('! {}'.format(ans))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()

C++ Code:

#include <bits/stdc++.h>

using namespace std;

int n, k;

vector<int> find_steps(const vector<int>& a) {
    vector<int> steps;
    for (int i = 0; i < n - 1; ++i) {
        if (a[i] == 1 && a[i + 1] == 0) steps.push_back(i);
    }
    return steps;
}


int main() {
    cin >> n >> k;
    string s; cin >> s;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) a[i] = (s[i] == 'L') ? 0 : 1;
    int maxi = 0, mini = 0;
    int cnt = 0;
    int last = -1;
    for (int i = n - 1; i >= 0; --i) {
        if (a[i] == 0) {
            ++cnt;
        } else {
            if (cnt == 0) continue;
            maxi += cnt;
            mini = max(cnt, last + 1);
            last = mini;
        }
    }
    if (k < mini || k > maxi) {
        cout << -1;
        return 0;
    }
    bool is_min = false;
    vector<int> have_step;
    for (int i = 0; i < k; ++i) {
        if (!is_min) {
            auto steps = find_steps(a);
            cout << min(int(steps.size()), maxi - k + i + 1) << ' ';
            int cur = 0;
            while (k - i - 1 < maxi && cur < steps.size()) {
                cout << steps[cur] + 1 << ' ';
                a[steps[cur]] = 0;
                a[steps[cur] + 1] = 1;
                ++cur;
                --maxi;
            }
            if (maxi == k - i - 1) {
                is_min = true;
                have_step = find_steps(a);
            }
        } else {
            int v = have_step.back();
            have_step.pop_back();
            cout << "1 " << v + 1;
            a[v] = 0;
            a[v + 1] = 1;
            if (v > 0 && a[v - 1] == 1) {
                have_step.push_back(v - 1);
            }
            if (v + 2 < n && a[v + 2] == 0) {
                have_step.push_back(v + 1);
            }
        }
        cout << '\n';
    }
}


Comments

Submit
0 Comments
More Questions

734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality